﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AdoHelper
{
    [Serializable]
    public class InsertBuilder   
    {
        private string tableName = "";
        public string TableName
        {
            get { return tableName; }
            set { tableName = value; }
        }

        private InsertClause insert = new InsertClause();
        public InsertClause Insert
        {
            get { return insert; }
        }

        private ValuesClause values = new ValuesClause();
        public ValuesClause Values
        {
            get { return values; }
        }


        private string resultSets = "";
        public string ResultSets
        {
            get { return resultSets; }
            set { resultSets = value; }
        }


        public void AddcolumnsValues(params KeyValuePair<string, string>[] pairs)
        {
            for (int i = 0; i < pairs.Length; i++)
            {
                this.insert.Add(pairs[i].Key);
                this.values.Add(pairs[i].Value);
            }
        }

        public string Statement
        {
            get
            {
                string s = "";
                if (this.insert.ToString() != "")
                {
                    s += "INSERT INTO " + tableName + " (" + this.insert.ToString() + ")\r\n";
                }
                if (this.resultSets != "")
                {
                    s += resultSets;
                }
                if (this.values != null)
                {
                    s += "VALUES (" + this.values.ToString() + ")\r\n";
                }
                return s;
            }
        }

        public void Reset()
        {
            this.tableName = "";
            this.resultSets = "";
            this.insert.Clear();
            this.values.Clear();
        }

        public override string ToString()
        {
            return this.Statement;
        }

    }
    [Serializable]
    public class InsertClause
    {
        private string totalExpressions = "";

        public void Add(params string[] expressions)
        {
            for (int i = 0; i < expressions.Length; i++)
            {
                if (expressions[i].Trim() != "")
                {
                    if (totalExpressions != "")
                    {
                        totalExpressions += ", ";
                    }
                    totalExpressions += expressions[i];
                }
            }
        }
        public void Clear()
        {
            this.totalExpressions = "";
        }
        public override string ToString()
        {
            return totalExpressions;
        }
    }
    [Serializable]
    public class ValuesClause
    {
        private string totalExpressions = "";

        public void Add(params string[] expressions)
        {
            for (int i = 0; i < expressions.Length; i++)
            {
                if (expressions[i].Trim() != "")
                {
                    if (totalExpressions != "")
                    {
                        totalExpressions += ", ";
                    }
                    totalExpressions += expressions[i];
                }
            }
        }
        public void Clear()
        {
            this.totalExpressions = "";
        }
        public override string ToString()
        {
            return totalExpressions;
        }
    }

}
